package com.xhsj.user.hr.system.web;

import com.xhsj.user.hr.base.BaseParam;
import com.xhsj.user.hr.process.dto.processDefinition.HandleAuditDto;
import com.xhsj.user.hr.system.dto.HandoverAuditDto;
import com.xhsj.user.hr.system.entity.TbHrApprovalDimissionApply;
import com.xhsj.user.hr.system.vo.DimissionApplyAuditTaskVo;
import com.xhsj.user.hr.system.vo.GetHandOverDataInfoByIdVo;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.beans.factory.annotation.Autowired;
import io.swagger.annotations.ApiOperation;
import com.baomidou.mybatisplus.core.metadata.IPage;

import java.util.*;
import com.xhsj.user.utils.Message;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;

import org.springframework.web.bind.annotation.RestController;


import com.xhsj.user.hr.system.service.TbHrApprovalDimissionHandoverApplyService;
import com.xhsj.user.hr.system.entity.TbHrApprovalDimissionHandoverApply;


/**
 * <p>
 * hr流程-离职交接申请表 前端控制器
 * </p>
 *
 * @author suxiaolin
 * @since Sep 9, 2020 3:32:18 PM
 */
@Slf4j
@Api(tags = "hr流程-离职交接申请表")
@RestController
@RequestMapping("/system/tbHrApprovalDimissionHandoverApply")
public class TbHrApprovalDimissionHandoverApplyController {

	@Autowired
    public TbHrApprovalDimissionHandoverApplyService service;


	/**
	 * 保存/修改
	 * @author suxiaolin
	 * @date Sep 9, 2020 3:32:18 PM
	 */
	@ApiOperation(value = "hr流程-离职交接申请表,保存/修改", notes = "hr流程-离职交接申请表,保存和修改方法")
	@RequestMapping(value = "/save", method = RequestMethod.POST)
//    @PreAuthorize("hasAuthority('tb:hr:approval:dimission:handover:apply:edit')")
	public Message save(@RequestBody TbHrApprovalDimissionHandoverApply data){
    	try {
    		service.saveData(data);
			return Message.success("成功");
		} catch (Exception e) {
			log.error("保存失败:{}",e);
        	return Message.error(1,e.getMessage());
		}

	}



	/**
	 * 查询待审核任务
	 * @author 苏小林
	 * @date 2020/9/4 13:22
	 */
	@ApiOperation(value = "查询待审核任务", notes = "查询待审核任务")
	@RequestMapping(value="/findDimissionHandoverApplyRunTask",method = RequestMethod.POST)
	@PreAuthorize("hasAuthority('tb:hr:approval:dimission:handover:apply:audit')")
	public Message findDimissionHandoverApplyRunTask(@RequestBody BaseParam baseParam) {
		try {
			IPage<DimissionApplyAuditTaskVo> iPage = service.findDimissionHandoverApplyRunTask(baseParam);
			return Message.success("成功",iPage.getRecords(),iPage.getTotal(),iPage.getPages());
		} catch (Exception e) {
			log.error("查询异常:{}",e);
			return Message.error(1,"失败");
		}
	}

	/**
	 * 查询历史任务
	 * @author 苏小林
	 * @date 2020/9/4 17:43
	 * @param: baseParam
	 * @return: com.xhsj.user.utils.Message
	 */
	@ApiOperation(value = "查询历史任务", notes = "查询历史任务")
	@RequestMapping(value="/findDimissionHandoverApplyHiTask",method = RequestMethod.POST)
	@PreAuthorize("hasAuthority('tb:hr:approval:dimission:handover:apply:audit')")
	public Message findDimissionHandoverApplyHiTask(@RequestBody BaseParam baseParam) {
		try {
			IPage<DimissionApplyAuditTaskVo> iPage = service.findDimissionHandoverApplyHiTask(baseParam);
			return Message.success("成功",iPage.getRecords(),iPage.getTotal(),iPage.getPages());
		} catch (Exception e) {
			log.error("查询异常:{}",e);
			return Message.error(1,"失败");
		}
	}

	/**
	 * 申请人确认
	 * @author 苏小林
	 * @date 2020/9/11 13:40
	 * @param: id
	 * @param: applyConfirmOpinion
	 * @return: com.xhsj.user.utils.Message
	 */
	@ApiOperation(value = "申请人确认", notes = "申请人确认")
	@RequestMapping(value="/applyConfirm",method = RequestMethod.GET)
	public Message applyConfirm(@RequestParam("id") Long id,@RequestParam("applyConfirmOpinion") String applyConfirmOpinion) {
		try {
			service.applyConfirm(id, applyConfirmOpinion);
			return Message.success("成功");
		} catch (Exception e) {
			log.error("异常:{}", e);
			return Message.error(1, "失败");
		}
	}


	@ApiOperation(value = "流程审核", notes = "流程审核")
	@RequestMapping(value = "/processAudit", method = RequestMethod.POST)
	public Message processAudit(@RequestBody HandoverAuditDto handoverAuditDto, BindingResult bindingResult) {

		if (bindingResult.hasErrors()) {
			log.info(bindingResult.getFieldError().getDefaultMessage());
			return Message.error(1,bindingResult.getFieldError().getDefaultMessage());
		}

		try {
			service.processAudit(handoverAuditDto);
			return Message.success("成功");
		} catch (Exception e) {
			log.error("保存失败:{}",e);
			return Message.error(1,e.getMessage());
		}

	}

	/**
	 * 保存/修改
	 * @author suxiaolin
	 * @date Sep 9, 2020 3:32:18 PM
	 */
	@RequestMapping(value = "/update", method = RequestMethod.POST)
//    @PreAuthorize("hasAuthority('tb:hr:approval:dimission:handover:apply:edit')")
	public Message update(@RequestBody HandoverAuditDto handoverAuditDto, BindingResult bindingResult){

		if (bindingResult.hasErrors()) {
			log.info(bindingResult.getFieldError().getDefaultMessage());
			return Message.error(1,bindingResult.getFieldError().getDefaultMessage());
		}

		try {

			TbHrApprovalDimissionHandoverApply apply = handoverAuditDto.getApply();
//			service.updateById(apply);
            service.updateData(apply);

			return Message.success("成功");
		} catch (Exception e) {
			log.error("保存失败:{}",e);
			return Message.error(1,e.getMessage());
		}

	}


	/**
	 * 通过id查询数据
	 * @author 苏小林
	 * @date 2020/9/10 9:50
	 */
	@ApiOperation(value = "通过id查询数据", notes = "通过id查询数据")
	@RequestMapping(value="/getDataInfoById",method = RequestMethod.GET)
	public Message getDataInfoById(@RequestParam("id") Long id) {
		try {
			TbHrApprovalDimissionHandoverApply getHandOverDataInfoByIdVo = service.getDataInfoById(id);
			return Message.success("成功",getHandOverDataInfoByIdVo);
		} catch (Exception e) {
			log.error("失败:{}",e);
			return Message.error(1,"失败");
		}

	}

	/**
	 * 通过id删除数据
	 * @author suxiaolin
	 * @date Sep 9, 2020 3:32:18 PM
	 */
	@ApiOperation(value = "hr流程-离职交接申请表删除", notes = "通过id删除hr流程-离职交接申请表信息。")
	@RequestMapping(value = "/delete", method = RequestMethod.GET)
    @PreAuthorize("hasAuthority('tb:hr:approval:dimission:handover:apply:delete')")
	public Message delete(@RequestParam(value="id") Long id) {
		try {
			service.removeById(id);
        	return Message.success("成功");
		} catch (Exception e) {
			log.error("删除失败:{}",e);
            return Message.error(1,"失败");
		}

	}

	/**
	 * 批量删除
	 * @author suxiaolin
	 * @date Sep 9, 2020 3:32:18 PM
	 */
	@ApiOperation(value = "批量删除", notes = "批量删除。")
	@RequestMapping(value = "/batchDelete", method = RequestMethod.POST)
	@PreAuthorize("hasAuthority('tb:hr:approval:dimission:handover:apply:delete')")
	public Message batchDelete(@RequestBody List<Long> list) {
		try {
			service.removeByIds(list);
    		return Message.success("成功");
		} catch (Exception e) {
    		log.error("删除失败:{}",e);
    		return Message.error(1,"失败");
		}
    }

    /**
    * 分页查询数据
    * @author suxiaolin
    * @date Sep 9, 2020 3:32:18 PM
    */
	@ApiOperation(value = "分页查询hr流程-离职交接申请表list", notes = "带参数查询,{\"page\": 1,\"size\": 5,......}。支持模糊查询")
	@RequestMapping(value="/findList",method = RequestMethod.POST)
    @PreAuthorize("hasAuthority('tb:hr:approval:dimission:handover:apply:view')")
	public Message findList(@RequestBody Map<String,Object> data) {
        try {
        	IPage<TbHrApprovalDimissionHandoverApply> selectPage = service.selectPage(data);
    		return Message.success("成功",selectPage.getRecords(),selectPage.getTotal(),selectPage.getPages());
        } catch (Exception e) {
			log.error("查询异常:{}",e);
    		return Message.error(1,"失败");
        }
	}

    /**
    * 查询所有数据
    * @author suxiaolin
    * @date Sep 9, 2020 3:32:18 PM
    */
	@ApiOperation(value = "查询hr流程-离职交接申请表所有数据list", notes = "查询hr流程-离职交接申请表所有数据list。")
	@RequestMapping(value="/findAll",method = RequestMethod.POST)
    @PreAuthorize("hasAuthority('tb:hr:approval:dimission:handover:apply:view')")
	public Message findAll(){
        return Message.success("成功",service.selectAll());
	}

    /**
    * 通过id获取实体类
    * @author suxiaolin
    * @date Sep 9, 2020 3:32:18 PM
    */
	@ApiOperation(value = "通过id获取hr流程-离职交接申请表实体类", notes = "通过id获取hr流程-离职交接申请表实体类。")
	@RequestMapping(value="/getById",method = RequestMethod.GET)
	public Message getById(@RequestParam(value="id") Long id) {
    	return Message.success("成功",service.getById(id));
	}

    /**
    * 通过id查询详细信息
    * @author suxiaolin
    * @date Sep 9, 2020 3:32:18 PM
    */
	@ApiOperation(value = "通过id获取实体类详细信息", notes = "通过id获取实体类详细信息。")
	@RequestMapping(value="/detail",method = RequestMethod.GET)
	public Message detail(@RequestParam(value="id") Long id) {
    	return Message.success("成功",service.getById(id));
	}

}

